package com.wizardClient;

import com.appclient.ClientWizardController;
import com.appclient.ControlledScreen;
import com.appclient.MainPanelController;
import java.util.HashMap;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;

/**
 *
 * @author Sawior
 */
public class ClientScreenController extends StackPane {

    private HashMap<String, Node> screens = new HashMap<>();
    private HashMap<String, ClientControlledScreen> controllers = new HashMap<>();
    private ClientWizardController clientWizardController;
    private String actual = "";

    public ClientScreenController(ClientWizardController clientWizardController) {
        super();
        this.clientWizardController = clientWizardController;
    }

    public void addScreen(String name, Node screen) {
        screens.put(name, screen);
    }

    public boolean loadScreen(String name, String resource) {
        try {
            FXMLLoader myLoader = new FXMLLoader(getClass().getClassLoader().getResource(resource));
            Parent loadScreen = (Parent) myLoader.load();
            ClientControlledScreen myScreenControler = ((ClientControlledScreen) myLoader.getController());
            myScreenControler.setScreenParent(this);
            addScreen(name, loadScreen);
            controllers.put(name, myScreenControler);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public boolean setScreen(final String name) {

        if (screens.get(name) != null) { //screen loaded
            final DoubleProperty opacity = opacityProperty();

            //Is there is more than one screen
            if (!getChildren().isEmpty()) {
                Timeline fade = new Timeline(
                        new KeyFrame(Duration.ZERO,
                        new KeyValue(opacity, 1.0)),
                        new KeyFrame(new Duration(500),
                        new EventHandler() {
                    @Override
                    public void handle(Event t) {
                        //remove displayed screen
                        getChildren().remove(0);
                        //add new screen
                        getChildren().add(0, screens.get(name));
                        Timeline fadeIn = new Timeline(
                                new KeyFrame(Duration.ZERO,
                                new KeyValue(opacity, 0.0)),
                                new KeyFrame(new Duration(400),
                                new KeyValue(opacity, 1.0)));
                        fadeIn.play();
                    }
                }, new KeyValue(opacity, 0.0)));
                fade.play();
            } else {
                //no one else been displayed, then just show
                setOpacity(0.0);
                getChildren().add(screens.get(name));
                Timeline fadeIn = new Timeline(
                        new KeyFrame(Duration.ZERO,
                        new KeyValue(opacity, 0.0)),
                        new KeyFrame(new Duration(2500),
                        new KeyValue(opacity, 1.0)));
                fadeIn.play();
            }
            actual = name;
            if(name.equals(ClientScreensFramework.FIVE_SCREEN)){
                ChooseParkingSpaceController cont = (ChooseParkingSpaceController) controllers.get(name);
                ChooseClientController client = (ChooseClientController) controllers.get(ClientScreensFramework.FIRST_SCREEN);
                ChooseCarController car = (ChooseCarController) controllers.get(ClientScreensFramework.SECOND_SCREEN);
                cont.loadSpacesStatus(client.getStartDate(), client.getEndDate(), car.getCar().getCarType());
            }
            if(name.equals(ClientScreensFramework.SIX_SCREEN)){
                EndPageController cont = (EndPageController) controllers.get(name);
                ChooseClientController client = (ChooseClientController) controllers.get(ClientScreensFramework.FIRST_SCREEN);
                ChooseCarController car = (ChooseCarController) controllers.get(ClientScreensFramework.SECOND_SCREEN);
                ChooseAccountingController acc = (ChooseAccountingController) controllers.get(ClientScreensFramework.THIRD_SCREEN);
                ChooseTarrifController tarrif = (ChooseTarrifController) controllers.get(ClientScreensFramework.FOUR_SCREEN);
                ChooseParkingSpaceController space = (ChooseParkingSpaceController) controllers.get(ClientScreensFramework.FIVE_SCREEN);
                cont.loadData(client.getClient(), client.getStartDate(), client.getEndDate(), car.getCar(), acc.getAccountingData(), tarrif.getTarrifId().getTarrifId(), space.getChoosedSpace().getNumber());
            }
            return true;
        } else {
            System.out.println("screen hasn't been loaded!\n");
            return false;
        }
    }

    public boolean unloadScreen(String name) {
        if (screens.remove(name) == null) {
            System.out.println("Screen didn't exist");
            return false;
        } else {
            return true;
        }
    }
    
    public String getActual(){
        return actual;
    }
    
    public ClientControlledScreen getController(String name){
        return this.controllers.get(name);
    }
}
